﻿Imports DevExpress.XtraGrid.Views.Grid
Public Class frmOnHand
    Dim _Mysql As New ClsSQLhelper
    Dim Strsql As String
    Dim On_Load As Boolean = True
    Dim Primary_Code As Integer
    Dim On_Load_Class As Boolean = True
    Dim On_Load_Group As Boolean = False
    Dim On_Combobox_Change As Boolean = True
    Dim DT_ItemCate As New DataTable
    Dim DT_ItemGroup As New DataTable
    Dim DT_Items As New DataTable
    Dim Flg_LoadZeroDetail As Boolean = False

#Region "Function"
    
    Sub Load_ItemStatus()
        Strsql = "Select Status_ID,Status_Name from itemstatus"
        Dim DT_ItemStatus As New DataTable
        DT_ItemStatus = _Mysql.GetMYSQLDataTable(Strsql, "itemstatus")
        With Me.CboItemStatus
            .DataSource = DT_ItemStatus
            .DisplayMember = "Status_Name"
            .ValueMember = "Status_ID"
        End With
    End Sub
    Sub Load_BudgetType()
        Try
            Dim DT_Budget As New DataTable
            Strsql = "select Budget_ID,Budget_Name  from budget order by Budget_ID "
            DT_Budget = _Mysql.GetMYSQLDataTable(Strsql, "sections")
            With Me.CboBudgetType
                .DataSource = DT_Budget
                .DisplayMember = "Budget_Name"
                .ValueMember = "Budget_ID"

            End With
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Sub Load_Data()
        Try
            Me.GridColumn2.FieldName = "fullItemcode"
            Me.GridColumn2.Caption = "หมายเลขครุภัณฑ์"
            Me.GridColumn2.OptionsColumn.ReadOnly = True
            Me.GridColumn12.FieldName = "Item_Name"
            Me.GridColumn12.Caption = "ชื่อครุภัณฑ์"
            Me.GridColumn12.OptionsColumn.ReadOnly = True
            Me.GridColumn13.FieldName = "Band"
            Me.GridColumn13.Caption = "ยี่ห้อ"
            Me.GridColumn13.OptionsColumn.ReadOnly = True
            Me.GridColumn14.FieldName = "Item_Model"
            Me.GridColumn14.Caption = "รุ่น (Model)"
            Me.GridColumn14.OptionsColumn.ReadOnly = True
            Me.GridColumn15.FieldName = "Product_From_Country"
            Me.GridColumn15.Caption = "ผลิตภัณฑ์จากประเทศ"
            Me.GridColumn15.OptionsColumn.ReadOnly = True
            Me.GridColumn16.FieldName = "Item_From_Company"
            Me.GridColumn16.Caption = "ซื้อจากบริษัท"
            Me.GridColumn16.OptionsColumn.ReadOnly = True
            Me.GridColumn17.FieldName = "serialno"
            Me.GridColumn17.Caption = "รหัสผลิตภัณฑ์"
            Me.GridColumn17.OptionsColumn.ReadOnly = True
            Me.GridColumn18.FieldName = "Item_EngName"
            Me.GridColumn18.Caption = "ชื่อครุภัณฑ์ (ภาษาอังกฤษ)"
            Me.GridColumn18.OptionsColumn.ReadOnly = True
            Me.GridColumn3.FieldName = "Budget_year"
            Me.GridColumn3.Caption = "ปีงบประมาณ"
            Me.GridColumn3.OptionsColumn.ReadOnly = True
            Me.GridColumn4.FieldName = "Budget_Name"
            Me.GridColumn4.Caption = "ได้รับจากงบ"
            Me.GridColumn4.OptionsColumn.ReadOnly = True
            Me.GridColumn5.FieldName = "Item_Price"
            Me.GridColumn5.Caption = "ราคาต่อหน่วย"
            Me.GridColumn5.OptionsColumn.ReadOnly = True
            Me.GridColumn6.FieldName = "Receive_Date"
            Me.GridColumn6.Caption = "วันที่ได้รับ"
            Me.GridColumn6.OptionsColumn.ReadOnly = True
            Me.GridColumn7.FieldName = "Department_Name"
            Me.GridColumn7.Caption = "แผนก/ฝ่าย"
            Me.GridColumn7.OptionsColumn.ReadOnly = True
            Me.GridColumn8.FieldName = "Ref_DocReceiveID"
            Me.GridColumn8.Caption = "เลขที่เอกสารับ"
            Me.GridColumn8.OptionsColumn.ReadOnly = True
            Me.GridColumn9.FieldName = "Ref_DocDrawID"
            Me.GridColumn9.Caption = "เลขที่เอกสารเบิก"
            Me.GridColumn9.OptionsColumn.ReadOnly = True
            Me.GridColumn10.FieldName = "Discharge"
            Me.GridColumn10.Caption = "จำหน่าย"
            Me.GridColumn10.OptionsColumn.ReadOnly = True
            Me.GridColumn1.Caption = "Item_ID"
            Me.GridColumn1.FieldName = "Item_ID"

            With GridItem

                Strsql = "Select Item_ID,Item_Type,Item_Code,Concat(Item_Type,Item_Code) as fullItemcode,Item_Name,Budget_year,Budget_Name,Item_Price,Receive_Date,Department_Name,Item_From_Company,Product_From_Country,Item_Model,Band,serialno,Item_EngName,"
                Strsql = Strsql & " Ref_DocReceiveID,Ref_DocDrawID,Discharge,Band,Item_EngName,Product_From_Country,Item_Model,Item_From_Company,Item_Desc"
                Strsql = Strsql & ",'' as companylist,'' as price_from_companylist,item_status,Status_Name "
                Strsql = Strsql & " from items "
                Strsql = Strsql & " left join budget on items.Item_Budget = budget.Budget_ID "
                Strsql = Strsql & " left join department on items.department = department.department_id "
                Strsql = Strsql & " left join typeitem on items.Item_Type = typeitem.Type_ID "
                Strsql = Strsql & " left join itemstatus on items.item_status = itemstatus.Status_ID "
                Select Case Me.TxtCode.Text
                    Case Is <> ""
                        If SearchCreteria.EditValue = 0 Then
                            Strsql = Strsql & " where  Concat(Item_Type,Item_Code)  like '" & Me.TxtCode.Text & "%' "
                        Else
                            Strsql = Strsql & " where Item_Name  like '%" & Me.TxtCode.Text & "%' "
                        End If
                        Me.GroupBudget.Enabled = False
                        Me.GroupItem.Enabled = False
                        Me.GroupPrice.Enabled = False
                        Me.GroupStatus.Enabled = False
                        Me.GroupReceiveDate.Enabled = False

                        Exit Try
                    Case Else
                        Me.GroupBudget.Enabled = True
                        Me.GroupItem.Enabled = True
                        Me.GroupPrice.Enabled = True
                        Me.GroupStatus.Enabled = True
                        Me.GroupReceiveDate.Enabled = True
                End Select

                Select Case ChkAllCategories.Checked
                    Case False
                        Strsql = Strsql & " where "
                    Case True


                End Select



                Select Case Me.ChkAllGroup.Checked
                    Case True
                        Strsql = Replace(Strsql, "where ", "")
                        'Exit Try
                    Case False

                        Dim DT_Section As New DataTable
                        Dim StrsqlSection As String = "Select section_id,ifnull(section_code,'') as section_code from sections where section_id = " & Me.CboGroup.SelectedValue
                        Dim StrSectionCOde As String = ""
                        DT_Section = _Mysql.GetMYSQLDataTable(StrsqlSection, "sections")

                        If DT_Section.Rows(0).Item("section_code").ToString <> "" Then
                            StrSectionCOde = DT_Section.Rows(0).Item("section_code").ToString
                        Else
                            StrSectionCOde = DT_Section.Rows(0).Item("section_id").ToString
                        End If
                        DT_Section.Dispose()

                        Strsql = Strsql & " typeitem.section = " & CboGroup.SelectedValue
                End Select


                Select Case Me.ChkAllAttribute.Checked
                    Case True
                        If Me.CboClass.Items.Count = 0 Then
                            Exit Try
                        End If
                    Case False
                        If CboAttribute.Items.Count <> 0 Then
                            Dim StrAttriute As String = ""
                            'For i = 1 To (4 - Me.CboAttribute.SelectedValue.ToString.Trim.Length)
                            '    StrAttriute = StrAttriute & "0"
                            'Next
                            Dim DT_Attribute As New DataTable
                            Dim StrsqlAttribute As String = "Select attribute_id,ifnull(attribute_code,'') as attribute_code from attribute where attribute_id = " & Me.CboAttribute.SelectedValue
                            Dim StrAttributeCOde As String = ""
                            DT_Attribute = _Mysql.GetMYSQLDataTable(StrsqlAttribute, "attribute")

                            If DT_Attribute.Rows(0).Item("attribute_code").ToString <> "" Then
                                StrAttributeCOde = DT_Attribute.Rows(0).Item("attribute_code").ToString
                            Else
                                StrsqlAttribute = DT_Attribute.Rows(0).Item("attribute_id").ToString
                            End If
                            DT_Attribute.Dispose()
                            
                            'StrAttriute = StrAttriute & Me.CboAttribute.SelectedValue.ToString.Trim
                            Strsql = Strsql & " and  right(Item_Type,4) = '" & StrAttributeCOde & "' "
                        Else
                            '  Exit Try
                        End If

                End Select

                Select Case ChkAllClass.Checked
                    Case True
                        ' Exit Try
                    Case False
                        If Me.CboAttribute.Items.Count > 0 Then
                            CboClass.Enabled = False
                            ChkAllClass.Enabled = False
                            CboAttribute.Enabled = True
                            ChkAllAttribute.Enabled = True
                        Else
                            CboClass.Enabled = True
                            ChkAllClass.Enabled = True
                            CboAttribute.Enabled = False
                            ChkAllAttribute.Enabled = False
                            Strsql = Strsql & " and  Item_Type = '" & CboClass.SelectedValue.ToString.Trim & "' "

                        End If

                End Select

                Select Case ChkAllBudgetType.Checked
                    Case True
                        '
                    Case False
                        If Me.CboBudgetType.Items.Count <> 0 Then
                            Strsql = Strsql & " and budget.Budget_ID =  " & Me.CboBudgetType.SelectedValue
                        End If

                End Select

                Select Case ChkNonDate.Checked
                    Case True
                        Select Case ChkAddNullReceiveDate.Checked
                            Case True
                                '
                            Case False
                                Strsql = Strsql & " and Receive_Date is not null "
                        End Select
                    Case False
                        Strsql = Strsql & " and  (Receive_Date between '" & Pn_Framework.MysqlDateTimeFormat(Me.DtpFDate.DateTime, False) & "' and '" & Pn_Framework.MysqlDateTimeFormat(Me.DtpEDate.DateTime, False) & "'"
                        Select Case ChkAddNullReceiveDate.Checked
                            Case True
                                Strsql = Strsql & " or Receive_Date is null)"
                            Case False
                                Strsql = Strsql & ")"
                        End Select
                End Select

                Select Case Me.ChkNonPrice.Checked
                    Case True
                        Select Case ChkAddNullPrice.Checked
                            Case True
                                '
                            Case False
                                Strsql = Strsql & " and Item_Price <> 0 "
                        End Select
                    Case False
                        Strsql = Strsql & " and  (Item_Price >= " & Me.TxtPriceFrom.Value & " and  Item_Price <= " & Me.TxtPriceTo.Value

                        Select Case ChkAddNullPrice.Checked
                            Case True
                                Strsql = Strsql & " or Item_Price =0)"
                            Case False
                                Strsql = Strsql & ")"
                        End Select

                End Select

                Select Case ChkAllStatus.Checked
                    Case True

                    Case False
                        If CboItemStatus.Items.Count <> 0 Then
                            Strsql = Strsql & " and item_status = " & CboItemStatus.SelectedValue
                        End If

                        Select Case chkDischarge.Checked
                            Case True
                                Select Case Me.TxtCode.Text
                                    Case Is <> ""
                                        If SearchCreteria.EditValue = 0 Then
                                            Strsql = Strsql & " or (Concat(Item_Type,Item_Code)  like '" & Me.TxtCode.Text & "%'  and  discharge = 'Y' )"
                                        Else
                                            Strsql = Strsql & " or  (Item_Name  like '%" & Me.TxtCode.Text & "%' and  discharge = 'Y' )"
                                        End If
                                        Exit Sub
                                End Select


                        End Select
                End Select
            End With
        Catch ex As Exception

            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_Data"
                .MemoErr_Description.Text = "SQL Error กรุณาตรวจสอบคำสั่ง"
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try

        Try
            DT_Items = _Mysql.GetMYSQLDataTable(Strsql, "Items")
            GridItem.DataSource = Nothing
            GridItem.DataSource = DT_Items
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_Data"
                .MemoErr_Description.Text = "SQL Error กรุณาตรวจสอบคำสั่ง"
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With



        End Try

    End Sub
    Sub Load_Categories()
        Try

            Strsql = "Select * from categories"
            DT_ItemCate = _Mysql.GetMYSQLDataTable(Strsql, "categories")
            With CboCategories
                .DataSource = DT_ItemCate
                .DisplayMember = "Cate_Name"
                .ValueMember = "Cate_ID"

            End With
        Catch ex As Exception
            frmDebug.lblFormName.Text = Me.Name
            frmDebug.lblFunctionName.Text = "Load_ItemGroup"
            frmDebug.MemoErr_Description.Text = ex.Message
            frmDebug.MemoSQL.Text = Strsql
            frmDebug.ShowDialog()
        End Try
    End Sub
    Sub Load_Group()
        Try
            On_Load_Group = True
            'Strsql = "Select Section_ID,Section_Name from sections order by section_id"
            Strsql = "select section_id,section_name  from sections where Cate_ID = '" & Me.CboCategories.SelectedValue & "' order by section_id "
            DT_ItemGroup = _Mysql.GetMYSQLDataTable(Strsql, "sections")
            With Me.CboGroup


                .DataSource = DT_ItemGroup
                .DisplayMember = "section_name"
                .ValueMember = "Section_ID"

                If DT_ItemGroup.Rows.Count = 0 Then
                    .DataSource = Nothing
                    .Text = ""
                    Me.ChkAllGroup.Checked = True
                    Me.ChkAllAttribute.Checked = True
                    Load_Data()
                    Flg_LoadZeroDetail = True
                    Me.CboAttribute.DataSource = Nothing
                    Me.GridItem.DataSource = Nothing
                    Me.CboClass.DataSource = Nothing
                    Me.CboAttribute.Enabled = False
                    Me.CboClass.Enabled = False
                    Me.CboGroup.Enabled = False

                    Exit Sub
                Else

                    Flg_LoadZeroDetail = False

                    Me.ChkAllGroup.Checked = False
                    Me.ChkAllAttribute.Checked = False
                    Me.CboAttribute.Enabled = True
                    Me.CboClass.Enabled = True
                    Me.CboGroup.Enabled = True
                    Me.CboGroup.SelectedIndex = 0

                End If

            End With
            On_Load_Group = False
        Catch ex As Exception
            frmDebug.lblFormName.Text = Me.Name
            frmDebug.lblFunctionName.Text = "Load_ItemGroup"
            frmDebug.MemoErr_Description.Text = ex.Message
            frmDebug.MemoSQL.Text = Strsql
            frmDebug.ShowDialog()
        End Try

    End Sub
    Sub Load_Attribute()
        Try
            If On_Load_Group = True Then Exit Sub
            On_Load_Class = True
            If Flg_LoadZeroDetail = True Then Exit Sub
            With Me.CboAttribute
                .DataSource = Nothing
                .Text = ""

                If Me.DT_ItemGroup.Rows.Count = 0 Then
                    GridItem.DataSource = Nothing
                    Exit Sub
                End If

                Dim DT_Itemclass As New DataTable
                '  Strsql = "Select Type_ID,Type_Name,Concat(trim(Type_ID),' - ',trim(Type_Name)) as fullclassname from typeitem where type_id = " & Me.CboGroup.SelectedValue
                Strsql = "Select attribute_id,attribute_name from attribute where  cate_id = " & Me.CboCategories.SelectedValue & " and section_id = " & Me.CboGroup.SelectedValue
                DT_Itemclass = _Mysql.GetMYSQLDataTable(Strsql, "typeitem")


                .DataSource = DT_Itemclass
                .DisplayMember = "attribute_name"
                .ValueMember = "attribute_id"

                If DT_Itemclass.Rows.Count = 0 Then
                    Me.ChkAllAttribute.Checked = True
                    Flg_LoadZeroDetail = True
                    Exit Sub
                Else
                    Flg_LoadZeroDetail = False
                End If
            End With
            On_Load_Class = False

        Catch ex As Exception
            frmDebug.lblFormName.Text = Me.Name
            frmDebug.lblFunctionName.Text = "Load_Class"
            frmDebug.MemoErr_Description.Text = ex.Message
            frmDebug.MemoSQL.Text = Strsql
            frmDebug.ShowDialog()
        End Try

    End Sub
    Sub Load_Class()
        Try
            If On_Load_Group = True Then Exit Sub
            On_Load_Class = True
            With Me.CboClass
                .DataSource = Nothing
                .Text = ""

                If Me.DT_ItemGroup.Rows.Count = 0 Then
                    GridItem.DataSource = Nothing
                    Exit Sub
                End If

                Dim DT_Itemclass As New DataTable
                Strsql = "Select Type_ID,Type_Name,Concat(trim(Type_ID),' - ',trim(Type_Name)) as fullclassname from typeitem where section = " & Me.CboGroup.SelectedValue
                ' Strsql = "Select attribute_id,attribute_name from attribute where  cate_id = " & Me.CboCategories.SelectedValue & " and section_id = " & Me.CboGroup.SelectedValue
                DT_Itemclass = _Mysql.GetMYSQLDataTable(Strsql, "typeitem")


                .DataSource = DT_Itemclass
                .DisplayMember = "Type_Name"
                .ValueMember = "Type_ID"

                If DT_Itemclass.Rows.Count = 0 Then
                    Me.ChkAllAttribute.Checked = True
                    Flg_LoadZeroDetail = True
                    Exit Sub
                Else
                    Flg_LoadZeroDetail = False
                End If
            End With
            On_Load_Class = False

        Catch ex As Exception
            frmDebug.lblFormName.Text = Me.Name
            frmDebug.lblFunctionName.Text = "Load_Class"
            frmDebug.MemoErr_Description.Text = ex.Message
            frmDebug.MemoSQL.Text = Strsql
            frmDebug.ShowDialog()
        End Try

    End Sub
#End Region

    Private Sub frmOnHand_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Load_Categories()
        Load_Group()
        Load_BudgetType()
        Load_Attribute()
        Load_Class()
        ' Load_Class()

        ' Load_BudgetYear()

      
        Load_ItemStatus()
        Load_Data()
        On_Load = False


    End Sub


    Private Sub MnFind_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles MnFind.ItemClick
        Load_Data()
    End Sub

    Private Sub MnAddNewItem_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles MnAddNewItem.ItemClick

        frmNewItem.ShowDialog()
    End Sub

    Private Sub MnExcelExport_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles MnExcelExport.ItemClick
        If (MsgBox("คุณต้องการส่งออกข้อมูลเป็น Excel ใช่หรือไม่", MsgBoxStyle.Question + MsgBoxStyle.YesNo)) = MsgBoxResult.No Then Exit Sub

        With SaveFileDialog1
            .Title = "ส่งออกข้อมูล"
            .FileName = "ข้อมูลสำรวจครุภัณฑ์.xls"
            .Filter = "Excel files (*.xls)|*.xls|" & "All files|*.*"
            .ShowDialog()
        End With

        GridItem.ExportToXls(SaveFileDialog1.FileName.ToString)
    End Sub
    Private Sub BtnEditItem_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BtnEditItem.ItemClick
        With FrmEditItem


            If Me.TxtCode.Text = "" Then Exit Sub
            If InStr(TxtCode.Text, "/") <> 0 Then


                .TxtItemCode.Text = TxtCode.Text.Substring(14, 3)

                .lblItemType.Text = Replace(Me.TxtCode.Text, "/" & TxtCode.Text.Substring(14, 3), "")
                '.TxtRunningNumber.Text = Me.Primary_Code
            Else
                .lblItemType.Text = TxtCode.Text

            End If
            .Primary_Code = Me.Primary_Code
            If .ShowDialog() = Windows.Forms.DialogResult.Cancel Then Exit Sub
            Load_Data()

        End With
    End Sub


    Private Sub CboGroup_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CboGroup.SelectedIndexChanged
        Me.TxtCode.Text = ""
        If On_Load = True Then Exit Sub
        If On_Load_Group = True Then Exit Sub
        On_Combobox_Change = True
        Load_Attribute()
        Load_Class()
        On_Combobox_Change = False
        Load_Data()
    End Sub



    Private Sub ChkAllGroup_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChkAllGroup.CheckedChanged
        If On_Combobox_Change = True Then Exit Sub

        If ChkAllGroup.Checked = True Then
            Me.ChkAllAttribute.Enabled = False
            Me.CboAttribute.Enabled = False
            Me.CboGroup.Enabled = False
            Me.ChkAllAttribute.Checked = True
            Me.ChkAddNullPrice.Checked = True
            'Me.ChkAddNullPrice.Enabled = False
            'Me.ChkAddNullReceiveDate.Enabled = False
            Me.ChkAddNullReceiveDate.Checked = True
        Else
            Me.ChkAllAttribute.Checked = False
            Me.ChkAllAttribute.Enabled = True
            Me.CboAttribute.Enabled = True
            Me.CboGroup.Enabled = True

            Me.ChkAddNullPrice.Checked = False
            'Me.ChkAddNullPrice.Enabled = True
            Me.ChkAddNullReceiveDate.Checked = False
            ' Me.ChkAddNullReceiveDate.Enabled = True
        End If
        Load_Data()
    End Sub

  

    Private Sub ChkAllBudgetType_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChkAllBudgetType.CheckedChanged
        If On_Combobox_Change = True Then Exit Sub
        If ChkAllBudgetType.Checked = True Then
            Me.CboBudgetType.Enabled = False

        Else
            Me.CboBudgetType.Enabled = True

        End If
        Load_Data()
    End Sub

    Private Sub ChkNonDate_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChkNonDate.CheckedChanged
        If On_Load = True Then Exit Sub
        If ChkNonDate.Checked = False Then
            DtpFDate.Enabled = True
            DtpFDate.DateTime = Now
            DtpEDate.Enabled = True
            DtpEDate.DateTime = Now
            Me.ChkAddNullReceiveDate.Enabled = True
            Me.ChkAddNullReceiveDate.Checked = True
        Else
            DtpFDate.Enabled = False

            DtpEDate.Enabled = False


            Me.ChkAddNullReceiveDate.Enabled = True
            Me.ChkAddNullReceiveDate.Checked = True

            Me.ChkAddNullPrice.Enabled = True
            Me.ChkAddNullPrice.Checked = True
        End If
        Load_Data()
    End Sub

    Private Sub ChkNonPrice_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChkNonPrice.CheckedChanged
        If On_Combobox_Change = True Then Exit Sub
        If ChkNonPrice.Checked = False Then
            TxtPriceFrom.Enabled = True
            TxtPriceTo.Enabled = True
            TxtPriceTo.Text = 9999999
            Me.ChkAddNullPrice.Enabled = True
            Me.ChkAddNullPrice.Checked = True
        Else
            TxtPriceFrom.Enabled = False
            TxtPriceTo.Enabled = False

            Me.ChkAddNullPrice.Enabled = True
            Me.ChkAddNullPrice.Checked = True
        End If
        Load_Data()
    End Sub

    Private Sub GridItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GridItem.Click

    End Sub

    Private Sub GridView1_CustomColumnDisplayText(ByVal sender As System.Object, ByVal e As DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs) Handles GridView1.CustomColumnDisplayText
        If e.Column.FieldName = "" Then
            e.DisplayText = e.RowHandle.ToString + 1
        End If
    End Sub

    Private Sub GridView1_CustomRowCellEditForEditing(ByVal sender As System.Object, ByVal e As DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs) Handles GridView1.CustomRowCellEditForEditing
        Try

            Dim Gv As GridView = sender
            'Dim TypeCODE As String = Gv.GetRowCellValue(e.RowHandle, Gv.Columns("Item_Type")).ToString()
            Dim CodeName As String
            If Me.SearchCreteria.EditValue = 0 Then
                CodeName = Gv.GetRowCellValue(e.RowHandle, Gv.Columns("fullItemcode")).ToString()
            Else
                CodeName = Gv.GetRowCellValue(e.RowHandle, Gv.Columns("Item_Name")).ToString()
            End If
            Primary_Code = Gv.GetRowCellValue(e.RowHandle, Gv.Columns("Item_ID")).ToString()
            Me.TxtCode.Text = CodeName
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub GridView1_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GridView1.DoubleClick
        With FrmEditItem


            If Me.TxtCode.Text = "" Then Exit Sub
            .TxtItemCode.Text = TxtCode.Text.Substring(14, 3)
            .lblItemType.Text = Replace(Me.TxtCode.Text, "/" & TxtCode.Text.Substring(14, 3), "")

            .ShowDialog()
        End With
    End Sub

    Private Sub LabelControl2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LabelControl2.Click

    End Sub



    Private Sub BtnDelete_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BtnDelete.ItemClick
        If (MsgBox("กรุณายืนยันการลบข้อมูล", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "ยืนยันการลบข้อมูล") = MsgBoxResult.No) Then Exit Sub
        If (MsgBox("การลบข้อมูลเมื่อยืนยันแล้วจะไม่สามารถนำกลับมาใหม่ได้ คุณยังต้องการยืนยันใช่หรือไม่", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "ยืนยันการลบข้อมูล") = MsgBoxResult.No) Then Exit Sub

        Try
            'Delete Item Information
            ' Strsql = "Delete from items where concat(item_type,item_code)='" & Me.TxtCode.Text.ToString.Trim & "'"
            Strsql = "Delete from items where item_id='" & Me.Primary_Code & "'"
            If _Mysql.MySQLExecute(Strsql) = -1 Then
                frmDebug.MemoErr_Description.Text = "SQL Error กรุณาตรวจสอบคำสั่ง"
                frmDebug.MemoSQL.Text = Strsql
                frmDebug.ShowDialog()
                Exit Sub
            Else
                'Delete Discharge Information
                Strsql = "Delete from discharge_item where concat(item_type,item_code)='" & Me.TxtCode.Text.ToString.Trim & "'"
                If _Mysql.MySQLExecute(Strsql) = -1 Then
                    frmDebug.MemoErr_Description.Text = "SQL Error กรุณาตรวจสอบคำสั่ง"
                    frmDebug.MemoSQL.Text = Strsql
                    frmDebug.ShowDialog()
                    Exit Sub
                End If

                MsgBox("ลบข้อมูลเรียบร้อยแล้ว", MsgBoxStyle.Information, "ผลการลบข้อมูล")
                Me.TxtCode.Text = ""
                Load_Data()
            End If



            'Add Log
            Strsql = "Insert into transections_logs (trans_code,trans_datetime,trans_computer,trans_item) values (3,'" & Pn_Framework.MysqlDateTimeFormat(Now, True) & "','" & Pn_Framework.Get_Computername & "','" & Me.Primary_Code & "') "
            Select Case (_Mysql.MySQLExecute(Strsql))
                Case -1
                    frmDebug.lblFormName.Text = Me.Name
                    frmDebug.lblFunctionName.Text = "BtnDelete_ItemClick"
                    frmDebug.MemoErr_Description.Text = "SQL Error กรุณาตรวจสอบคำสั่ง"
                    frmDebug.MemoSQL.Text = Strsql
                    frmDebug.ShowDialog()
            End Select
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub ChkAllStatus_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChkAllStatus.CheckedChanged
        If On_Load = True Then Exit Sub
        If ChkAllStatus.Checked = True Then
            Me.CboItemStatus.Enabled = False
            Me.chkDischarge.Enabled = False
        Else
            Me.CboItemStatus.Enabled = True
            Me.chkDischarge.Enabled = True
        End If
        Load_Data()
    End Sub

    Private Sub BtnCurrentSQL_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BtnCurrentSQL.ItemClick
        DlgCurrentSQL.MemoCurrentSQL.Text = Strsql
        DlgCurrentSQL.ShowDialog()
    End Sub

    Private Sub TxtCode_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtCode.EditValueChanged

    End Sub

    Private Sub CboCategories_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CboCategories.SelectedIndexChanged
        Me.TxtCode.Text = ""
        If On_Load = True Then Exit Sub
        On_Combobox_Change = True
        Load_Group()
        If Flg_LoadZeroDetail = True Then Exit Sub
        Load_Attribute()
        Load_Class()
        Load_Data()
        On_Combobox_Change = False
    End Sub

    Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChkAllCategories.CheckedChanged

        ' If On_Combobox_Change = True Then Exit Sub
        If ChkAllCategories.Checked = True Then
            CboCategories.Enabled = False
            ChkAllGroup.Checked = True
            ChkAllGroup.Enabled = False
        Else
            ChkAllGroup.Checked = False
            CboCategories.Enabled = True
            ChkAllGroup.Enabled = True
        End If
        Load_Data()

    End Sub

    Private Sub chkDischarge_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkDischarge.CheckedChanged
        If On_Load = True Then Exit Sub

        Load_Data()
    End Sub

    Private Sub ChkAddNullReceiveDate_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChkAddNullReceiveDate.CheckedChanged
        If On_Load = True Then Exit Sub
        Load_Data()
    End Sub

    Private Sub ChkAddNullPrice_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChkAddNullPrice.CheckedChanged
        If On_Combobox_Change = True Then Exit Sub
        Load_Data()
    End Sub

    Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
        If On_Combobox_Change = True Then Exit Sub
        Load_Data()
    End Sub


    Private Sub CboAttribute_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CboAttribute.SelectedIndexChanged
        Me.TxtCode.Text = ""
        If On_Load_Class = True Then Exit Sub
        On_Combobox_Change = True
        Me.TxtCode.Text = ""
        On_Combobox_Change = False

        If On_Load = False Then
            Load_Data()
        End If
    End Sub

    Private Sub CboClass_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CboClass.SelectedIndexChanged
        Me.TxtCode.Text = ""
        If On_Load_Class = True Then Exit Sub
        If Flg_LoadZeroDetail = True Then Exit Sub
        On_Combobox_Change = True
        If On_Load = False Then
            Load_Data()
        End If
        On_Combobox_Change = False
    End Sub

    Private Sub ChkAllAttribute_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChkAllAttribute.CheckedChanged
        If On_Combobox_Change = True Then Exit Sub
        If ChkAllAttribute.Checked = True Then
            Me.CboAttribute.Enabled = False

        Else
            Me.CboAttribute.Enabled = True

        End If
        Load_Data()
    End Sub

    Private Sub ChkAllClass_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChkAllClass.CheckedChanged
        If On_Combobox_Change = True Then Exit Sub
        If ChkAllClass.Checked = True Then
            Me.CboClass.Enabled = False

        Else
            Me.CboClass.Enabled = True

        End If
        Load_Data()
    End Sub

    Private Sub CboItemStatus_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CboItemStatus.SelectedIndexChanged
        If On_Load = True Then Exit Sub

        Load_Data()
    End Sub

    Private Sub CboBudgetType_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CboBudgetType.SelectedIndexChanged
        If On_Load = True Then Exit Sub

        Load_Data()
    End Sub

    Private Sub BtnDefault_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BtnDefault.ItemClick
        Me.TxtCode.Text = ""
        Me.GroupBudget.Enabled = True
        Me.GroupItem.Enabled = True
        Me.GroupPrice.Enabled = True
        Me.GroupReceiveDate.Enabled = True
        Me.GroupStatus.Enabled = True
        Load_Data()

    End Sub
End Class